import logging
from pathlib import Path
from pydantic import BaseModel
import cnvpytor

logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s")
logger = logging.getLogger("ClinCNVseq")


class App(BaseModel):
    sample: str
    bam_file: Path
    pytor_file: Path
    bin_size: int
    thread: int

    def do_app(self):
        logger.info("Do app to %s", self.pytor_file)
        app = cnvpytor.Root(str(self.pytor_file), create=True, max_cores=self.thread)
        logger.info("Reading read depth from %s", self.bam_file)
        app.rd([str(self.bam_file)])
        logger.info("Calculating histograms")
        app.calculate_histograms([self.bin_size])
        logger.info("Creating partition")
        app.partition([self.bin_size], use_mask=True)

    def run(self):
        if not self.pytor_file.exists():
            self.do_app()
